package dhadi.util;

import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {

	private static final String DEFAULT_DATE_FORMAT = "yyyy-dd-MM HH:mm:ss ZZ";
	private static final String DEFAULT_PLAY_LOG_DIR = "./play-log/";

	public static Point delta(Point a, Point b)
	{
		return new Point(Util.delta(a.x, b.x), Util.delta(a.y, b.y));		
	}
	
	public static int delta(int a, int b)
	{
		int delta = a - b;
		if (delta < 0)
			delta = -1 * delta;
		return delta;
	}

	public static String getFormattedDate() {
		DateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		return df.format(new Date(System.currentTimeMillis()));
	}
	
	public static MoveWriter createMoveWriter() throws IOException {
		String fileName = Util.getFormattedDate().replaceAll(" ", "_");
		fileName = fileName.replaceAll("\\+", "_");
		fileName = fileName.replaceAll(":", "_");
		fileName = fileName + ".txt";
		MoveWriter mw = new MoveWriter(new File(DEFAULT_PLAY_LOG_DIR,fileName));
		return mw;
	}
	
	public static MoveReader createMoveReader(String fileName) throws IOException {
	    return new MoveReader(new File(fileName));
	}
	
	public static boolean isEmpty(String string) {
	    if (string == null || string.trim().length() == 0) return true;
	    else return false;
	}

    public static boolean isEmptyJson(String jsonString) {
        if (Util.isEmpty(jsonString)) return true;
        if (jsonString.equalsIgnoreCase("null")) return true;
        else return false;
    }
}